package auth

import (
	"core/coredto"
	"core/coreentity"
	"core/coreservice"
	"core/corestruct"
	"core/myerrors"
	"github.com/todocoder/go-stream/stream"
)

type platMenu struct {
	platId int64
	ctx    *corestruct.HttpContext
	lmenu  *[]coreentity.HtMenu
	lpage  *[]coreentity.HtPage
	lauth  *[]coreentity.HtPageAuth
}

func (p *platMenu) getMenu() (*[]coredto.TreeViewNode, error) {
	var db = p.ctx.DB
	var tx = db.Model(&coreentity.HtMenu{}).
		Where("htplat_id=?", p.platId).
		Select("id,name,pid,code,lx,htpage_id,imgurl,p_1,p_2,p_3,p_4,sortno")
	var svrmenu = &coreservice.ListService[coreentity.HtMenu]{}
	var err error = nil
	p.lmenu, err = svrmenu.List(p.ctx, tx)
	if err != nil {
		return nil, myerrors.NewByErr(err)
	}
	var lpageid = make([]int64, 0)
	for _, v := range *p.lmenu {
		if v.HtpageId > 0 {
			lpageid = append(lpageid, v.HtpageId)
		}
	}

	tx = db.Model(&coreentity.HtPage{}).
		Where("id in ?", lpageid).
		Select("id,name, code, imgurl,p_1,p_2,p_3,p_4 ")
	var svrpage = &coreservice.ListService[coreentity.HtPage]{}
	p.lpage, err = svrpage.List(p.ctx, tx)
	if err != nil {
		return nil, myerrors.NewByErr(err)
	}

	tx = db.Model(&coreentity.HtPageAuth{}).
		Where("htpage_id in ?", lpageid).
		Select("id,name, code, htpage_id,urls,p_1,p_2,p_3,p_4,sortno ")
	var svrpageauth = &coreservice.ListService[coreentity.HtPageAuth]{}
	p.lauth, err = svrpageauth.List(p.ctx, tx)
	if err != nil {
		return nil, myerrors.NewByErr(err)
	}
	var r = make([]coredto.TreeViewNode, 0)
	p.addChildMenu(&r, nil)
	return &r, nil
}
func (p *platMenu) addChildMenu(r *[]coredto.TreeViewNode, tn *coredto.TreeViewNode) {
	pid := int64(0)
	if tn != nil {
		pid = tn.Id
	}
	var sm = stream.Of(*(p.lmenu)...).
		Filter(func(i coreentity.HtMenu) bool {
			return i.Pid == pid
		}).
		Sorted(func(a, b coreentity.HtMenu) bool {
			return a.Sortno < b.Sortno
		})
	sm.ForEach(func(menu coreentity.HtMenu) {

		if menu.Lx == "目录" {
			var tnc = coredto.TreeViewNode{
				Id:          menu.Id,
				Pid:         menu.Pid,
				Code:        menu.Code,
				Name:        menu.Name,
				Lx:          menu.Lx,
				Imgurl:      menu.Imgurl,
				P1:          menu.P1,
				P2:          menu.P2,
				P3:          menu.P3,
				P4:          menu.P4,
				Expanded:    false,
				HasChildren: false,
				Children:    nil,
				Obj:         "",
			}
			var child = make([]coredto.TreeViewNode, 0)
			tnc.Children = &child

			if tn == nil {
				p.addChildMenu(r, &tnc)
				*r = append(*r, tnc)

			} else {
				if tn.Children == nil {
					var child = make([]coredto.TreeViewNode, 0)
					tn.Children = &child
				}
				*tn.Children = append(*tn.Children, tnc)

			}
		} else if menu.Lx == "菜单" {

			var tnc = p.createPageMenu(&menu)
			if tn == nil {
				*r = append(*r, *tnc)

			} else {

				*tn.Children = append(*tn.Children, *tnc)
			}
		}
	})

}
func (p *platMenu) createPageMenu(menu *coreentity.HtMenu) *coredto.TreeViewNode {
	//菜单
	var smpage = stream.Of((*p.lpage)...).
		Filter(func(item coreentity.HtPage) bool {
			return item.Id == menu.HtpageId
		}).FindFirst()
	if !smpage.IsPresent() {
		panic("致命错误")
	}
	var mpage, ok = smpage.Get()
	if !ok {
		panic("致命错误")
	}

	var tnpage = &coredto.TreeViewNode{
		Id:          menu.Id,
		Pid:         menu.Pid,
		Code:        mpage.Code,
		Name:        mpage.Name,
		Lx:          menu.Lx,
		Imgurl:      menu.Imgurl,
		P1:          mpage.P1,
		P2:          mpage.P2,
		P3:          mpage.P3,
		P4:          mpage.P4,
		Expanded:    false,
		HasChildren: false,
		Children:    nil,
		Obj:         "",
	}
	var child = make([]coredto.TreeViewNode, 0)
	tnpage.Children = &child

	var smauth = stream.Of(*(p.lauth)...).
		Filter(func(item coreentity.HtPageAuth) bool {
			return item.HtpageId == mpage.Id
		}).Sorted(func(a, b coreentity.HtPageAuth) bool {
		return a.Sortno < b.Sortno
	})
	smauth.ForEach(func(mauth coreentity.HtPageAuth) {

		var tnauth = &coredto.TreeViewNode{
			Id:          mauth.Id,
			Pid:         tnpage.Id,
			Code:        mauth.Code,
			Name:        mauth.Name,
			Lx:          "权限",
			Imgurl:      "",
			P1:          "",
			P2:          "",
			P3:          "",
			P4:          "",
			Expanded:    false,
			HasChildren: false,
			Children:    nil,
			Obj:         mauth.Urls,
		}
		*tnpage.Children = append(*tnpage.Children, *tnauth)
	})
	return tnpage
}
